<template>
  <view>
  <view class="search-box">
    <uni-search-bar  @input="input" :radius="100" cancelButton="none" :focus="true"/>
  </view>
  <!-- 搜索建议列表 -->
  <view class="sugg-list" v-if="searchResults.length!=0">
    <view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoGoods_detail(item)">
      <view class="goods-name">{{item.goods_name}}</view>
      <uni-icons type="forward"></uni-icons>
    </view>
  </view>
  <!-- 历史记录列表 -->
  <view class="history-list" v-else>
    <view class="history-item1">
      <text>搜索历史</text>
      <uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
    </view>
    <!-- 历史记录 -->
    <view class="history-item2">
        <uni-tag v-for="(item,index) in historys" :key="index" :text="item" inverted="true" @click="godetail(item)"></uni-tag>
    </view>
  </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        //搜索关键词
        kw:'',
        //搜索建议列表
        searchResults:[],
        //历史记录
        history:[],
      };
    },
    onLoad() {
      //加载时获取本地存储的历史记录
      this.history =JSON.parse(uni.getStorageSync('kw') || '[]') 
    },
    methods:{
      input(value){
        clearTimeout(this.timer)
        this.timer = setTimeout(()=>{
          this.kw = value
          this.getkw_data()
          // 保存历史记录
          this.saveSearchHistory()
        },500)
      },
      async getkw_data(){
        if(this.kw==''){
          this.searchResults = []
          return
        }
        const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
        if(res.meta.status!=200) return uni.$showMsg()
        // let a = ['未查询到相关信息']
        // this.searchResults = res.message? res.message : a
        this.searchResults = res.message
      
    },
    gotoGoods_detail(item){
      uni.navigateTo({
        url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
      })
    },
    saveSearchHistory(){
      const set = new Set(this.history)
      //从set中删除输入的值
      set.delete(this.kw)
      //在set中添加输入的值
      set.add(this.kw)
      //将set对象转化为数值重新赋值给history
      this.history = Array.from(set)
      // 将history存储到localstorage
      uni.setStorageSync('kw',JSON.stringify(this.history))
    },
    cleanHistory(){
      this.history = []
      uni.setStorageSync('kw','[]')
    },
    godetail(item){
      uni.navigateTo({
        url:'/subpkg/goods_detail/goods_detail?query='+ item
      })
    }
},
    computed: {
      historys() {
        // 注意：由于数组是引用类型，所以不要直接基于原数组调用 reverse 方法，以免修改原数组中元素的顺序
            // 而是应该新建一个内存无关的数组，再进行 reverse 反转
        return [...this.history].reverse() 
      }
    }
    
  }

</script>

<style lang="scss">
.search-box{
position: sticky;
top: 0;
z-index: 999;
}
.uni-searchbar{
  display: flex;
  // flex-direction: row;
  position: relative;
  padding: 16px;
  background-color: #c00000;
}
.sugg-list{
  background-color: #ffffff;
  padding: 0 5px;
  .sugg-item{
    display: flex;
    align-items: center;
    padding: 13px 0;
    border-bottom: 1px solid #efefef;
    justify-content: space-between;
    .goods-name{
      //文本不允许换行
      white-space: nowrap;
      //溢出部分隐藏
      overflow: hidden;
      //文本溢出后用...代替
      text-overflow: ellipsis;
      font-size: 12px;
  }}
}
.history-list{
  padding: 0 5px;
}
.history-item1{
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 40px;
  border-bottom: 1px solid #efefef;
}
.history-item2{
  display: flex;
  flex-wrap: wrap;
  .uni-tag {
      margin-top: 5px;
      margin-right: 8px;
  }
}
</style>
